package com.mj.torrentsearch.activity;

import java.util.Map;

import com.SYY.cbsdk.ui.AppControl;
import com.dyk.hfsdk.dao.util.DevListener;
import com.dyk.hfsdk.ui.Access;
import com.mj.torrentsearch.R;
import com.mj.torrentsearch.bean.TorrentDto;
import com.mj.torrentsearch.service.TorrentsearchService;
import com.mj.torrentsearch.util.NetUtils;
import com.mj.torrentsearch.widget.LoadingDialog;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.method.ArrowKeyMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView.BufferType;
/**
 * 详情页
 * @author zhaominglei
 * @date 2015-5-10
 * 
 */
public class TorrentDetailActivity extends Activity implements OnClickListener,DevListener {
	@SuppressWarnings("unused")
	private static final String TAG = TorrentDetailActivity.class.getSimpleName();
	private TorrentsearchService torrentsearchService = new TorrentsearchService();
	private ImageView goHome;
	private LinearLayout miniAdLinearLayout; //迷你广告
	private LoadingDialog loadingDialog; // 加载框
	private TextView nameView; //名称
	private EditText resultView; //下载地址
	private Button appOffersButton; //推荐应用
	private String name; //种子名称
	private String detailURL; //种子详情页地址
	private Access access;
	private AppControl appControl;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_torrent_detail);
		init();
	}

	private void init() {
		goHome = (ImageView)findViewById(R.id.torrentdetail_gohome);
		miniAdLinearLayout = (LinearLayout)findViewById(R.id.torrentdetail_miniAdLinearLayout);
		nameView = (TextView)findViewById(R.id.torrentdetail_name);
		resultView = (EditText)findViewById(R.id.torrentdetail_result);
		appOffersButton = (Button)findViewById(R.id.torrentdetail_appOffersButton);
		
		goHome.setOnClickListener(this);
		appOffersButton.setOnClickListener(this);
		loadingDialog = new LoadingDialog(this);
		//利用 EditText代替 TextView实现自由复制
		//参考：http://blog.csdn.net/jaycee110905/article/details/8762274
		resultView.setFocusableInTouchMode(true);
		resultView.setFocusable(true);
		resultView.setLongClickable(true);
		resultView.setSingleLine(false);
		resultView.setMovementMethod(ArrowKeyMovementMethod.getInstance());
		
		appControl = AppControl.getInstance();
		appControl.init(TorrentDetailActivity.this, MainActivity.MUMAYI_APPID, MainActivity.MUMAYI_APPCHANNEL);
		appControl.loadPopAd(TorrentDetailActivity.this);
		appControl.showPopAd(TorrentDetailActivity.this, 60 * 1000);
		appControl.showInter(TorrentDetailActivity.this, miniAdLinearLayout);
		
		access = Access.getInstance();
		// 初始化带player_id
		access.init(TorrentDetailActivity.this, MainActivity.MUMAYI_APPID, MainActivity.MUMAYI_APPCHANNEL);
		// 设置初始积分                                                              
		access.setdefaultSCORE(this, 100);
		// 设置获取积分的监听
		access.setAppListener(this, this);
		
		name = getIntent().getStringExtra("name");
		detailURL = getIntent().getStringExtra("detailURL");
		if (name == null || name.equals("") || detailURL == null || detailURL.equals("")) {
			Toast.makeText(getApplicationContext(), R.string.data_error, Toast.LENGTH_SHORT).show();
			TorrentDetailActivity.this.finish();
		}
		nameView.setText(name);		
		getTorrentDetail();
	}

	private void getTorrentDetail() {
		if (NetUtils.isConnected(getApplicationContext())) {
			loadingDialog.show();
			new TorrentDetailAsyncTask().execute("");
		} else {
			Toast.makeText(getApplicationContext(), R.string.net_error, Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.torrentdetail_gohome:
			TorrentDetailActivity.this.finish();
			break;
			
		case R.id.appOffersButton:
			access.openWALL(TorrentDetailActivity.this);
			break;

		default:
			break;
		}
	}

	@Override
	public void onBackPressed() {
		TorrentDetailActivity.this.finish();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		appControl.close(this);
	}
	
	public class TorrentDetailAsyncTask extends AsyncTask<String, String, TorrentDto> {
		@Override
		protected TorrentDto doInBackground(String... params) {
			return torrentsearchService.getTorrentDetail(getApplicationContext(), name, detailURL);
		}

		@Override
		protected void onPostExecute(TorrentDto result) {
			loadingDialog.dismiss();
			if (result != null) {
				StringBuilder detail = new StringBuilder();
				if (TorrentDto.SUCCESS_SUCCESS.equals(result.getSuccess())) {
					detail.append("磁力链接 ").append("\n\n").append(result.getDownloadURL_magnet()).append("\n\n");
					detail.append("迅雷链接  ").append("\n\n").append(result.getDownloadURL_thunder()).append("\n\n");
					Map<String,String> summaryMap = result.getSummaryMap();
					if (summaryMap != null && !summaryMap.isEmpty()) {
						for (Map.Entry<String, String> entry: summaryMap.entrySet()) {
							String key = entry.getKey();
							String value = entry.getValue();
							detail.append(key).append(":").append(value).append("\n\n");
						}
					}
				} else {
					detail.append("由于用户投诉或者受版权方要求保护，该条目具体内容不予展示。");
				}
				resultView.setText(detail.toString(), BufferType.SPANNABLE);
			}
		}
	}
	
	@Override
	public void onDevSucceed(int eggs) {
		System.out.println("积分获取成功:" + eggs);
	}

	@Override
	public void onDevFailed(String message) {
		System.out.println("积分获取失败:" + message);
	}

	@Override
	public void onClose(Context context) {
		((Activity) context).finish();
	}	
}
